#include "AES.hpp"

/*
 * documentation in AES.hpp
 */

void AES::setKey(const Block &keyblock) {
	if (keyblock.getLength() != 16) throw GENERICEXCEPTION("Key length must be 16 bytes (128 bits)");
	AES_set_encrypt_key(keyblock.getRO(), 128, &encKey);
	AES_set_decrypt_key(keyblock.getRO(), 128, &decKey);
}

Block AES::encrypt(const Block &plaintext) const {
	if (plaintext.getLength() != 16) throw GENERICEXCEPTION("Block length must be 16 bytes (128 bits)");
	Block Ciphertext(16);
	AES_encrypt(plaintext.getRO(), Ciphertext.get(), &encKey);
	return Ciphertext;
}

Block AES::decrypt(const Block &ciphertext) const {
	if (ciphertext.getLength() != 16) throw GENERICEXCEPTION("Block length must be 16 bytes (128 bits)");
	Block Plaintext(16);
	AES_decrypt(ciphertext.getRO(), Plaintext.get(), &decKey);
	return Plaintext;
}

unsigned int AES::getBlockSize() const {
	return 16;
}

